Re: [GENERAL] Two variable passed to PL/Function and on is NULL - Mailing list pgsql-general

From Herouth Maoz
Subject Re: [GENERAL] Two variable passed to PL/Function and on is NULL
Date
Msg-id l03130302b38aaffbe209@[147.233.159.109]
Whole thread Raw
In response to [GENERAL] Two variable passed to PL/Function and on is NULL  (Stuart Rison <stuart@ludwig.ucl.ac.uk>)
Responses Re: [GENERAL] Two variable passed to PL/Function and on is NULL  (Stuart Rison <stuart@ludwig.ucl.ac.uk>)
List pgsql-general
At 15:44 +0300 on 14/06/1999, Stuart Rison wrote:


> i) Is it the case that if you pass to variables to a postgres function and
> one is NULL, the function cannot tell which one?
> ii) Is there a workaround of some kind (in particular in pl/pgsql)?
> [Currently I am copying the table into a temp table and updating all NULL
> values to a token value.]

I think you will do alright in pl/pgsql. Take for example the following
function which returns 1000 when its argument is null:

create function null1000( int4 ) returns int4 as '
DECLARE
  the_arg alias for $1;
BEGIN
  IF the_arg IS NULL THEN
     RETURN 1000;
  ELSE
     RETURN the_arg;
  END IF;
END;
' language 'plpgsql';

I tested it on the following table:

testing=> select * from test1;
nm
--
 4
 8

16
32

(6 rows)

And this is the result I got:

testing=> select null1000( nm ) from test1;
null1000
--------
       4
       8
    1000
      16
      32
    1000
(6 rows)

The problem arises if you try to pass a literal NULL to the function:

testing=> select null1000( NULL );
ERROR:  typeidTypeRelid: Invalid type - oid = 0

This is because the NULL doesn't have a type of int4. I am not even sure
this is a bug. In any case, it should work alright for normal NULLS. In the
same vein, you can ask whether the arguments in your function are null and
return your boolean properly.

Herouth

--
Herouth Maoz, Internet developer.
Open University of Israel - Telem project
http://telem.openu.ac.il/~herutma



pgsql-general by date:

Previous
From: Stuart Rison
Date:
Subject: [GENERAL] Two variable passed to PL/Function and on is NULL
Next
From: Martin Weinberg
Date:
Subject: Scalability to very large databases under Linux